/**
 * --------------------------------------------------------------------------
 * Hi.js (v1.0.0): sticktop.js
 * require jquery
 * --------------------------------------------------------------------------
 */
!function($, window, document, undefined) {
    var StickUp = function(elem, opts) {
        // console = console || {log: $.noop};
        // console = {log: $.noop};
        var left, lastScrollTop = 0, scroll = 0, scrollDir = "", scrollDistance = 0, active = !1,
            bottom = !1, hold = !1, disabled = !1, landscape = !1, portrait = !1, stickyHeight = 0,
            outerHeight = 0, currentOuterHeight = 0, viewportHeight = 0, scrollBottom = 0,
            elementOffset = 0, elementOffsetBottom = 0, $element = $(), topMargin = 0, offset = 0,
            $placeholder = $('<div style="margin-top:0;margin-bottom:0; padding:0"></div>'),
            $parent = $(), stickpoints = {
            top: 0,
            bottom: 0,
            custom: []
        }, options = {
            scrollHide: !1,
            lazyHeight: 0,
            topMargin: "auto",
            keepInWrapper: !1,
            wrapperSelector: "",
            zIndex: 99,
            syncPosition: !1,
            namespaceClass: "stuckElement",
            fixedClass: "isStuck",
            disableOn: function() {
                return !0
            }
        }, getTopMargin = function() {
            return "auto" === options.topMargin ?
                parseInt($element.css("marginTop")) :
                isNaN(options.topMargin) && options.topMargin.search("px") > 0 ?
                    parseInt(options.topMargin.replace("px", "")) :
                    isNaN(parseInt(options.topMargin)) ?(console.log("incorrect argument, ignored."),
            0) : parseInt(options.topMargin)
        }, unStick = function() {
            // console.log("unStick()"),
            $placeholder.remove(),
            $element.removeClass(options.fixedClass).css({
                maxWidth: "",
                marginTop: "",
                marginLeft: "",
                marginRight: "",
                position: "",
                top: "",
                left: "",
                right: "",
                bottom: "",
                width: ""
            }),
            active = !1,
            bottom = !1,
            hold = !1,
            options.syncPosition && syncMargins()
        }, holdIt = function(forceBottom) {
            // console.log("holdIt()"),
            $element.before($placeholder.css("height", outerHeight));
            var offsetParent = $placeholder.offsetParent();
            if (forceBottom) {
                $element.css({
                    position: "absolute"
                });
                var topOffset = $parent.offset().top + $parent.outerHeight() - offsetParent.offset().top - currentOuterHeight - parseInt($parent.css("paddingBottom"))
            }
            // console.log($parent.offset().top + $parent.outerHeight()),
            // console.log(offsetParent.offset().top - currentOuterHeight),
            $element.css({
                position: "absolute",
                marginTop: topMargin,
                bottom: "",
                left: $placeholder.position().left,
                top: forceBottom ? topOffset : $element.offset().top - offsetParent.offset().top - topMargin
            })
        }, stickIt = function() {
            // console.log("stickIt()"),
            active = !0,
            $element.before($placeholder.css("height", outerHeight)),
            $element.addClass(options.fixedClass);
            var topDistance = -offset;
            $element.css({
                marginTop: topMargin,
                position: "fixed",
                top: topDistance + "px",
                left: "",
                right: "",
                bottom: ""
            })
        }, stickAtBottom = function() {
            // console.log("stickAtBottom"),
            $element.before($placeholder.css("height", outerHeight)),
            $element.addClass(options.fixedClass);
            var bottomDistance = -offset;
            $element.css({
                marginTop: topMargin,
                position: "fixed",
                top: "",
                left: "",
                right: "",
                bottom: bottomDistance
            })
        }, syncWidth = function() {
            $placeholder.width() !== $element.outerWidth() && $element.outerWidth($placeholder.outerWidth())
        }, syncPosition = function() {
            left = $placeholder.offset().left,
            left !== $element.offset().left,
            $element.offset({
                left: left
            })
        }, syncMargins = function() {
            $placeholder.css({
                "margin-left": $element.css("margin-left"),
                "margin-right": $element.css("margin-left")
            }),
            $element.css({
                "margin-left": $placeholder.css("margin-left"),
                "margin-right": $placeholder.css("margin-right")
            })
        }, stickUpScrollHandlerFn = function(event) {
            if (!options.disableOn())
                return void (disabled || (console.log("disable"),
                unStick(),
                disabled = !0));
            if (disabled && (disabled = !1),
            !options.keepInWrapper || $parent.is(":visible")) {
                if (scroll = $(event.target).scrollTop(),
                scrollDir = scroll >= lastScrollTop ? "down" : "up",
                scrollDistance = Math.abs(lastScrollTop - scroll),
                viewportHeight = $(window).outerHeight(),
                scrollBottom = scroll + viewportHeight,
                lastScrollTop = scroll,
                elementOffset = $element.offset().top,
                stickyHeight = parseInt($element.outerHeight() + topMargin) + parseInt($element.css("marginBottom")),
                active || hold || bottom || (outerHeight = parseInt($element.outerHeight(!0)),
                bottom || hold ? stickpoints.top = parseInt($placeholder.offset().top) : stickpoints.top = parseInt($element.offset().top),
                left = parseInt($element.offset().left) + 5),
                currentOuterHeight = parseInt($element.outerHeight()) + parseInt($element.css("margin-bottom")) + topMargin,
                options.keepInWrapper ? stickpoints.bottom = $parent.offset().top + $parent.outerHeight() - parseInt($parent.css("paddingBottom")) : stickpoints.bottom = $(document).outerHeight(),
                elementOffsetBottom = $element.offset().top + currentOuterHeight,
                stickyHeight > viewportHeight)
                    portrait = !0,
                    landscape && (offset = 0,
                    hold && holdIt(),
                    landscape = !1),
                    hold && "up" === scrollDir && elementOffset - topMargin >= scroll && (console.log("sticktop"),
                    stickIt(),
                    active = !0,
                    bottom = !1,
                    hold = !1),
                    !active && !bottom && (!options.keepInWrapper || options.keepInWrapper && scrollBottom <= stickpoints.bottom) && scrollBottom >= elementOffsetBottom - topMargin && (console.log("stickAtBottom"),
                    stickAtBottom(),
                    bottom = !0,
                    active = !0,
                    hold = !1),
                    (!hold && active && bottom && "up" === scrollDir || !hold && active && !bottom && "down" === scrollDir && elementOffsetBottom >= scrollBottom) && (console.log("holdIt"),
                    holdIt(),
                    active = !1,
                    bottom = !1,
                    hold = !0),
                    scrollBottom >= stickpoints.bottom && options.keepInWrapper && (!bottom && !hold || parseInt(elementOffsetBottom - topMargin) !== parseInt(stickpoints.bottom)) && (console.log("forceBottom"),
                    holdIt(!0),
                    active = !1,
                    bottom = !0,
                    hold = !0);
                else if (landscape = !0,
                portrait && (hold && holdIt(),
                portrait = !1),
                offset = options.scrollHide ? stickyHeight + options.lazyHeight : options.lazyHeight,
                (!active && !bottom && scroll >= stickpoints.top - topMargin + offset || bottom && hold && elementOffset - topMargin + offset >= scroll) && (console.log("sticktop"),
                stickIt(),
                active = !0,
                bottom = !1,
                hold = !1),
                options.keepInWrapper && parseInt(elementOffsetBottom - topMargin) !== parseInt(stickpoints.bottom) && scroll >= stickpoints.bottom - currentOuterHeight + offset && (console.log("forceBottom p"),
                holdIt(!0),
                active = !1,
                bottom = !0,
                hold = !0),
                active) {
                    var topValue = parseInt($element.css("top"));
                    if ("up" === scrollDir && 0 !== topValue) {
                        var newTopValue = scrollDistance > -topValue ? 0 : topValue + scrollDistance;
                        $element.css("top", newTopValue + "px")
                    } else if ("down" === scrollDir && topValue > -offset) {
                        var newTopValue = scrollDistance > offset + topValue ? -offset : topValue - scrollDistance;
                        $element.css("top", newTopValue + "px")
                    }
                }
                (active || hold || bottom) && scroll <= stickpoints.top - topMargin && (console.log("unstick"),
                unStick()),
                (active || hold || bottom) && syncWidth(),
                (options.syncPosition && active || hold) && syncPosition()
            }
        }, stickUpResponsiveHandlerFn = function(event) {
            hold && (holdIt(),
            bottom = !1),
            // console.log("resize"),
            stickUpScrollHandlerFn(event)
        }, initialize = function(elem, opts) {
            $element = $(elem),
            $element.addClass(options.namespaceClass),
            opts && $.extend(!0, options, opts),
            topMargin = null !== options.topMargin ? getTopMargin() : 0,
            options.lazyHeight && (topMargin += options.lazyHeight),
            options.keepInWrapper ? ("" !== options.wrapperSelector && ($parent = $element.closest(options.wrapperSelector)),
            $parent.length || ($parent = $element.parent())) : $parent = $("body"),
            options.zIndex && $element.css("z-index", options.zIndex),
            syncPosition && syncMargins(),
            $(window).on("scroll.stickUp", stickUpScrollHandlerFn),
            $(window).on("resize.stickUp", stickUpResponsiveHandlerFn),
            stickUpScrollHandlerFn({
                target: document
            })
        };
        initialize.call(this, elem, opts)
    };
    $.fn.stickUp = function(options) {
        return this.each(function() {
            new StickUp(this,options)
        })
    }
}(jQuery, window, document);